//package com.eastseeway.radioproject.mq;
//
//import android.text.TextUtils;
//import android.util.Log;
//
//import androidx.annotation.NonNull;
//
//import com.eastseeway.radioproject.R;
//import com.eastseeway.radioproject.RadioApplication;
//import com.eastseeway.radioproject.base.Const;
//import com.rabbitmq.client.AMQP;
//import com.rabbitmq.client.AlreadyClosedException;
//import com.rabbitmq.client.Channel;
//import com.rabbitmq.client.Connection;
//import com.rabbitmq.client.ConnectionFactory;
//import com.rabbitmq.client.DefaultConsumer;
//import com.rabbitmq.client.DeliverCallback;
//import com.rabbitmq.client.Envelope;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeoutException;
//
///**
// * Created by Harry on 2017/8/17
// */
//public class RabbitMQClient {
//    private final String TAG = "RabbitMQ";
//
//    public static String EXCHANGE_NAME;
//    public static String EXCHANGE_TYPE;
//
//    private final String FLAG_SEND = "send";
//    private final String FLAG_RECEIVE = "receive";
//    private List<String> exchangeTypeList = new ArrayList<>(4);
//
//    private static RabbitMQClient singleton;
//    private final ConnectionFactory factory;
//    private Connection connection;
//    private Map<String, Channel> channelMap = new HashMap<>();
//
//    private RabbitMQClient() {
//        factory = new ConnectionFactory();
//
//        factory.setHost(RadioApplication.getInstance().getString(R.string.mq_address)); // 服务器ip
//        factory.setPort(Const.MQ_PORT); // 端口
//        factory.setUsername(Const.MQ_USERNAME); // 用户名
//        factory.setPassword(Const.MQ_PASSWORD); // 密码
//        factory.setVirtualHost(Const.MQ_VIRTUAL_HOST);
//    }
//
//    public static RabbitMQClient getInstance() {
//        if (singleton == null) {
//            synchronized (RabbitMQClient.class) {
//                if (singleton == null) {
//                    singleton = new RabbitMQClient();
//                }
//            }
//        }
//        return singleton;
//    }
//
//    public void sendPropertyMessage(String msg,  String exchangeName, String routingKey){
//        if (factory == null ) {
//            Log.e("12121","factory 为空 ");
//            return;
//        }
//        try {
//            Connection connection = factory.newConnection();
//            Channel channel = connection.createChannel();
//            try {
//                // 创建消息的properties
//                AMQP.BasicProperties.Builder propertiesBuilder = new AMQP.BasicProperties.Builder();
//                propertiesBuilder.deliveryMode(2); // 设置消息持久化
//                propertiesBuilder.contentType("application/json");
//                propertiesBuilder.priority(0); // 设置消息优先级
//                propertiesBuilder.contentEncoding("UTF-8");
//                Map<String, Object> propertiesMap = new HashMap<>();
//                propertiesMap.put("__ContentTypeId__", "java.lang.Object");
//                propertiesMap.put("__KeyTypeId__", "java.lang.String");
//                propertiesMap.put("__TypeId__", "org.jeecg.common.base.BaseMap");
//                propertiesBuilder.headers(propertiesMap);
//                AMQP.BasicProperties properties = propertiesBuilder.build();
//
//                channel.basicPublish(exchangeName, routingKey, properties, msg.getBytes());
//            } finally {
//                if (channel != null) {
//                channel.close();
//            }
//            if (connection != null) {
//                connection.close();
//            }
//        }}catch(Exception e){
//            System.out.println(e.getMessage());
//        }
//
//    }
//
//    public void receivePropertyMessage(String exchangeName,String queueName,String exchangeType,ResponseListener listener) throws IOException, TimeoutException {
//        Connection connection = factory.newConnection();
//        Channel channel = connection.createChannel();
//        channel.exchangeDeclare(exchangeName, exchangeType,true); // 声明一个转发器
//        channel.queueBind(queueName, exchangeName, queueName); // 绑定一个到转发器
//        Log.e("12121", exchangeName+" "+queueName);
//        DeliverCallback deliverCallback  = (consumerTag, message) -> {
//            String result = new String(message.getBody(), StandardCharsets.UTF_8);
//            Log.e("12121","MQCallback >>>"+result);
//            listener.receive(result);
////            ComMessage comMessage=new ComMessage();
////            comMessage.code="1";// .setCode("1");
////            comMessage.message=result;
//            // 创建一个事件
////            MessageEvent event = new MessageEvent();
////            event.setMessage(JSONUtil.toJsonStr(comMessage));
//
//            // 通过EventBus发送事件
////            EventBus.getDefault().post(event);
//        };
//
//        // 启动基本消费，并传入回调接口c
//        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
//        });
//    }
//
//    /**
//     * first
//     * 不定义转换器（默认匿名转换器），发送消息到指定队列中
//     */
//    public void sendQueueMessage(String message, String queueName) throws IOException, TimeoutException, AlreadyClosedException {
//        if (connection == null || !connection.isOpen()) {
//            connection = factory.newConnection();
//        }
//
//        if (!channelMap.containsKey(FLAG_SEND + queueName)) {
//            Channel channel = connection.createChannel();
//            channel.queueDeclare(queueName, false, false, false, null);
//            channelMap.put(FLAG_SEND + queueName, channel);
//        }
//
//        channelMap.get(FLAG_SEND + queueName).basicPublish("", queueName, null, message.getBytes());
//
////        FileInputStream fileInputStream = new FileInputStream(new File("D:/test.txt"));
////        channelMap.get(FLAG_SEND + queueName).basicPublish("", queueName, null, fileInputStream.read);
//        Log.d(TAG, "队列-" + queueName + "-发送消息=====" + message);
//    }
//
//    /**
//     * second
//     * 自定义默认转换器名称和类型，不指定队列，消息根据routingkey路由选择队列（fanout类型转换器例外，发送到绑定该转换器的所有队列中）
//     */
//    public void sendRoutingKeyMessage(String message, String routingkey) throws IOException, TimeoutException, AlreadyClosedException {
//        if (TextUtils.isEmpty(EXCHANGE_NAME) && !exchangeTypeList.contains(EXCHANGE_TYPE)) {
//            throw new NullPointerException("请先设置默认转换器名称和正确类型，否则请调用指定转换器名称和类型的方法");
//        }
//        sendQueueRoutingKeyMessage(message, routingkey, EXCHANGE_NAME, EXCHANGE_TYPE);
//    }
//
//    /**
//     * third
//     * 自定义转换器名称和类型，指定队列，消息根据routingkey路由选择队列（fanout类型转换器例外，发送到绑定该转换器的所有队列中）
//     */
//    public void sendQueueRoutingKeyMessage(String message, String routingKey, @NonNull String exchangeName,
//                                           String exchangeType) throws IOException, TimeoutException, AlreadyClosedException {
//        if (connection == null || !connection.isOpen()) {
//            connection = factory.newConnection();
//        }
//
//        if (!channelMap.containsKey(FLAG_SEND + routingKey + exchangeType)) {
//            Channel channel = connection.createChannel();
//            if (!TextUtils.isEmpty(exchangeName) && exchangeTypeList.contains(exchangeType)) {
//                channel.exchangeDeclare(exchangeName, exchangeType);
//            } else {
//                channel.queueDeclare(routingKey, false, false, false, null);
//            }
//            channelMap.put(FLAG_SEND + routingKey + exchangeType, channel);
//        }
//
//        AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()    //设置消息属性
//                .contentType("text/plain")
//                .deliveryMode(2)
//                .priority(1)
//                .build();
//
//        channelMap.get(FLAG_SEND + routingKey + exchangeType).basicPublish(exchangeName, routingKey, props, message.getBytes());
//        Log.d(TAG, "路由-" + routingKey + "-发送消息=====" + message);
//    }
//
//
//
//    /**
//     * 对应发送：first
//     * 不定义转换器（默认匿名装换器），指定队列，获取实时和缓存在队列中的消息
//     */
//    public void receiveQueueMessage(final String queueName, final ResponseListener listener)
//            throws IOException, TimeoutException, AlreadyClosedException {
//        if (TextUtils.isEmpty(queueName)) {
//            throw new NullPointerException("队列名字不能为空");
//        }
//        receiveQueueRoutingKeyMessage(queueName, "", listener);
//    }
//
//    /**
//     * 对应发送： second,third
//     * 自定义默认转换器名称和类型，不指定队列，监听符合routingKey的消息，只能获取实时消息
//     */
//    public void receiveRoutingKeyMessage(final String routingKey, final ResponseListener listener)
//            throws IOException, TimeoutException, AlreadyClosedException {
//        if (TextUtils.isEmpty(routingKey)) {
//            throw new NullPointerException("转换器路由不能设置为空");
//        }
//        receiveQueueRoutingKeyMessage("", routingKey, listener);
//    }
//
//    /**
//     * 对应发送：second,third
//     * 自定义默认非持久化转换器名称和类型，自定义队列，监听符合routingKey的消息，获取实时和缓存在队列中的消息
//     */
//    public void receiveQueueRoutingKeyMessage(final String queueName, final String routingKey, final ResponseListener listener)
//            throws IOException, TimeoutException, AlreadyClosedException {
//        if (TextUtils.isEmpty(EXCHANGE_NAME) || !exchangeTypeList.contains(EXCHANGE_TYPE)) {
//            throw new NullPointerException("请先设置默认转换器名称和正确类型，否则请调用指定转换器名称和类型的方法");
//        }
//        receiveQueueRoutingKeyMessage(queueName, routingKey, EXCHANGE_NAME, EXCHANGE_TYPE, listener);
//    }
//
//    /**
//     * 对应发送：second,third
//     * 自定义非持久化转换器名称和类型，自定义队列，监听符合routingKey的消息，获取实时和缓存在队列中的消息
//     */
//    public void receiveQueueRoutingKeyMessage(String queueName, final String routingKey, String exchangeName, String exchangeType, final ResponseListener listener)
//            throws IOException, TimeoutException, AlreadyClosedException {
//        if (!TextUtils.isEmpty(routingKey)) {
//            if (TextUtils.isEmpty(exchangeName) || !exchangeTypeList.contains(exchangeType)) {
//                throw new NullPointerException("转换器名称不能为空并且转换器类型必须正确");
//            }
//        }
//
//        if (connection == null || !connection.isOpen()) {
//            connection = factory.newConnection();
//        }
//
//        if (!channelMap.containsKey(FLAG_RECEIVE + routingKey + queueName)) {
//            final Channel channel = connection.createChannel();
//            //自定义队列名称，还是匿名队列
////            if (TextUtils.isEmpty(queueName)) {
////                queueName = channel.queueDeclare().getQueue();
////            } else {
////                channel.queueDeclare(queueName, false, false, false, null);
////            }
//            //绑定转换器，使用路由筛选消息
//            if (!TextUtils.isEmpty(routingKey)) {
//                channel.exchangeDeclare(exchangeName, exchangeType,true);
//                channel.queueBind(queueName, exchangeName, routingKey);  //设置绑定
//            }
//            //监听队列
////            channel.basicConsume(queueName, true, (s, delivery) -> {
////                String s1 = new String(delivery.getBody());
////                Log.e("12121","DeliverCallback >>> "+s1);
////            }, s -> {
////                Log.e("12121","CancelCallback >>> ");
////            });
//            channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope,
//                                           AMQP.BasicProperties properties, byte[] body) {
//                    String message = new String(body, StandardCharsets.UTF_8);
//                    if (listener != null) {
//                        listener.receive(message);
//                    }
//                    Log.e("12121","DeliverCallback >>> "+message);
////                    channel.basicAck(envelope.getDeliveryTag(), false);  //消息应答
//                }
//            });
//            channelMap.put(FLAG_RECEIVE + routingKey + queueName, channel);
//        }
//    }
//
//
//    /**
//     * 关闭所有资源
//     */
//    public void close() {
//        for (Channel next : channelMap.values()) {
//            if (next != null && next.isOpen()) {
//                try {
//                    next.close();
//                } catch (IOException | TimeoutException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        channelMap.clear();
//        if (connection != null && connection.isOpen()) {
//            try {
//                connection.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    public interface ResponseListener {
//        void receive(String message);
//    }
//}
